package top.honlnk.czuan.service.impl.module.add;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.honlnk.czuan.common.constant.UpdateMorphemeCountConstant;
import top.honlnk.czuan.mapper.AddMapper;
import top.honlnk.czuan.mapper.get.GetMorphemeTranslationsMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class AddWordSIModule {
    @Autowired
    private AddMapper addMapper;

    @Autowired
    private GetMorphemeTranslationsMapper getMTMapper;

    public void addRoot(Integer wordId, Integer rootId) {
        addMapper.addWordRootRelation(wordId, rootId);
        // 更新表示词根被继承的数量：+1
        addMapper.updateRootCount(rootId, UpdateMorphemeCountConstant.INCREMENT);
    }

    public void addPrefix(Integer wordId, Integer prefixId) {
        addMapper.addWordPrefixRelation(wordId, prefixId);
        addMapper.updatePrefixCount(prefixId, UpdateMorphemeCountConstant.INCREMENT);
    }

    public void addSuffix(Integer wordId, Integer suffixId) {
        addMapper.addWordSuffixRelation(wordId, suffixId);
        addMapper.updateSuffixCount(suffixId, UpdateMorphemeCountConstant.INCREMENT);
    }

    public void addTranslation(Integer wordId, Integer translationId) {
        addMapper.addWordTranslationRelation(wordId, translationId);
    }

    /**
     * 根据词根id列表，获取词根对应的翻译id的二维JSON列表，然后转成字符串返回
     *
     * @param rootIdList JSON列表，存储的是与这个方法相关的所有的root的id
     * @return JSON字符串，存储的是与这个方法相关的所有的root的id对应的翻译id，二维列表：将每个词根的翻译id都区分开存放，以方便吗后续展示
     */
    public String getRTIdListJson(@NotNull List<Integer> rootIdList) {
        Map<Integer, List<Integer>> rTIdsMap = new HashMap<>();
        for (Integer rootId : rootIdList) {
            List<Integer> rTIdList = getMTMapper.getRTIds(rootId);
            rTIdsMap.put(rootId, rTIdList);
        }

        return JSONUtil.toJsonStr(rTIdsMap);
    }

    public String getPFTIdListJson(@NotNull List<Integer> prefixIdList) {
        Map<Integer, List<Integer>> pFTIdsMap = new HashMap<>();
        for (Integer prefixId : prefixIdList) {
            List<Integer> pFTIdList = getMTMapper.getPTIds(prefixId);
            pFTIdsMap.put(prefixId, pFTIdList);
        }
        return JSONUtil.toJsonStr(pFTIdsMap);
    }

    public String getSFTIdListJson(@NotNull List<Integer> suffixIdList) {
        Map<Integer, List<Integer>> sFTIdsMap = new HashMap<>();
        for (Integer suffixId : suffixIdList) {
            List<Integer> sFTIdList = getMTMapper.getSTIds(suffixId);
            sFTIdsMap.put(suffixId, sFTIdList);
        }
        return JSONUtil.toJsonStr(sFTIdsMap);
    }

    public String getPartAndTIdListJson(@NotNull List<Integer> translationIdList) {

        List<Map<String, Object>> partAndTIdsList = getMTMapper.getPartAndTIdList(translationIdList);

        Map<Integer, JSONArray> partAndTIdsMap = new HashMap<>();
        for (Map<String, Object> partAndTIds : partAndTIdsList) {
            Integer partId = (Integer) partAndTIds.get("part_id");
            Integer tIdsJsonArray = (Integer) partAndTIds.get("id");

            partAndTIdsMap.computeIfAbsent(partId, k -> new JSONArray()).add(tIdsJsonArray);
        }
        return JSONUtil.toJsonStr(partAndTIdsMap);
    }
}
